package com.iceframework.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;

public class CommonUtils {

	private static final String APPLICATION_FILE = "application.properties";

	public static Environment ENV_PROPERTIES = null;

	public static Map<String, String> imageContentType = new HashMap<>();

	static {
		imageContentType.put("jpg", "image/jpeg");
		imageContentType.put("jpeg", "image/jpeg");
		imageContentType.put("png", "image/png");
		imageContentType.put("tif", "image/tiff");
		imageContentType.put("tiff", "image/tiff");
		imageContentType.put("ico", "image/x-icon");
		imageContentType.put("bmp", "image/bmp");
		imageContentType.put("gif", "image/gif");
	}

	/**
	 * 以springboot方试 读取properties文件
	 * 
	 * @param key
	 * @return
	 */
	public static String getSpringBootProperties(String key) {
		if (ENV_PROPERTIES == null) {
			ENV_PROPERTIES = SpringContextUtils.getBean(Environment.class);
		}
		return ENV_PROPERTIES.getProperty(key);
	}

	/**
	 * 读取properties文件
	 * 
	 * @param key
	 * @return
	 * @throws IOException
	 */
	public static String getProperties(String key) {
		try {
			Properties propert = new Properties();
			propert.load(CommonUtils.class.getClassLoader().getResourceAsStream(APPLICATION_FILE));
			return propert.getProperty(key);
		} catch (Exception e) {
			System.out.println("获取PROPERTIES参数失败:" + ExceptionUtils.getStackTraceAsString(e));
			return null;
		}
	}

	/**
	 * 获取Resource下文件
	 * 
	 * @param filePath
	 * @return
	 * @throws Exception
	 */
	public static InputStream getResourceInputStream(String filePath) throws Exception {
		ClassPathResource classPathResource = new ClassPathResource(filePath);
		InputStream inputStream = classPathResource.getInputStream();
		return inputStream;
	}

	/**
	 * 生成随机数字和字母
	 * 
	 * @param length
	 * @return
	 */
	public static String getStringRandom(int length) {

		String val = "";
		Random random = new Random();

		// 参数length，表示生成几位随机数
		for (int i = 0; i < length; i++) {

			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			// 输出字母还是数字
			if ("char".equalsIgnoreCase(charOrNum)) {
				// 输出是大写字母还是小写字母
				int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
				val += (char) (random.nextInt(26) + temp);
			} else if ("num".equalsIgnoreCase(charOrNum)) {
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}

	/**
	 * 直接用文件流来处理有更好的通用性
	 * 
	 * @param url
	 * @param httpServletResponse
	 * @throws Exception 
	 * @throws FileNotFoundException
	 */
	public static void showPicture(String url, HttpServletResponse httpServletResponse) throws Exception {
		FileInputStream inputStream = null;
		OutputStream toClient = null;
		try {
			httpServletResponse.setDateHeader("Expires", 0L);
			httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
			httpServletResponse.setHeader("Pragma", "no-cache");
			httpServletResponse.setContentType("image/jpeg");
			File image = new File(url);
			inputStream = new FileInputStream(image);
			int length = inputStream.available();
			byte data[] = new byte[length];
			httpServletResponse.setContentLength(length);
			String fileName = image.getName();
			String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
			httpServletResponse.setContentType(CommonUtils.imageContentType.get(fileType));
			inputStream.read(data);
			toClient = httpServletResponse.getOutputStream();
			toClient.write(data);
			toClient.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(toClient != null) {
				toClient.close();
			}
			if(inputStream != null) {
				inputStream.close();
			}
		}
	}
}
